//login.js
//获取应用实例
var app = getApp();
Page({
  data: {
    remind: '加载中',
    help_status: false,
    userid_focus: false,
    passwd_focus: false,
    shopid_focus: false,
    phone_focus: false,
    userid: '',
    passwd: '',
    shopid: '',
    phone: '',
    angle: 0,
    HideDetailInform: true,
    HideEventMask: true,
    user: [],
    shopid: [],
    shopidValue: 0,
    shopsex:['男','女'],
    shopsexValue: 0,
    shopname: [],
    shopnameValue: 0,
    pian: [],
    pianValue: 0,
    sheng: [],
    shengValue: 0,
    shi: [],
    shiValue: 0,
  },
  onReady: function () {
    var _this = this;
    setTimeout(function () {
      _this.setData({
        remind: ''
      });
    }, 1000);
    wx.onAccelerometerChange(function (res) {
      var angle = -(res.x * 30).toFixed(1);
      if (angle > 14) { angle = 14; }
      else if (angle < -14) { angle = -14; }
      if (_this.data.angle !== angle) {
        _this.setData({
          angle: angle
        });
      }
    });
  },
  onLoad: function () {
    var _this = this;
    _this.setData({
      'user': app._user,
    });
    this.GetAllUser();
  },
  onShow: function() {
    let loginData = wx.getStorageSync('login')
    if (loginData) {
      wx.switchTab({
        url: '../index/index',
      })
    }
  },
  GetAllUser: function () {
    var that = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetRegister'
      },
      success: (res) => {
        wx.hideToast();
        that.setData({
          shopid: res.data.ShopID,
          shopname: res.data.ShopName,
          pian: res.data.Pian,
          sheng: res.data.Sheng,
          shi: res.data.Shi,
        })
        wx.hideToast();
      },
      fail: function (res) {
        wx.hideToast();
        app.showErrorModal(res.errMsg);
      }
    })
  },
  formBindsubmit: function (e) {
    var _this = this;
    var formData = e.detail.value;
    console.log(formData)
    if (formData.account == '') {
      app.showErrorModal('帐号不能为空', '提醒')
      return false
    } else if (formData.phone == '') {
      app.showErrorModal('邀请码不能为空', '提醒')
      return false
    } else {
      wx.showLoading({
        title: '登陆中',
      })
      wx.request({
        url: app._server + '/Weixin_Hodna/WxLogin.ashx',
        data: {
          tel: formData.account,
          code: formData.phone,
          method: 'Login'
        },
        success: function (res) {
          wx.hideLoading()
          let data = res.data
          if (data.length == 0) {
            wx.showModal({
              title: '提示',
              content: '登陆失败，请确认帐号或邀请码是否正确',
              showCancel: false,
              confirmColor: '#cc0000'
            })
          } else {
            wx.setStorageSync('login', data)
            wx.switchTab({
              url: '../index/index',
            })
          }
        },
        fail: function (res) {
          console.log(res)
        }
      });
    }
  },
  useridInput: function (e) {
    this.setData({
      userid: e.detail.value
    });
  },
  passwdInput: function (e) {
    this.setData({
      passwd: e.detail.value
    });
  },
  shopidInput: function (e) {
    this.setData({
      shopid: e.detail.value
    });
  },
  PhoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    });
  },

  inputFocus: function (e) {
    if (e.target.id == 'userid') {
      this.setData({
        'userid_focus': true
      });
    } else if (e.target.id == 'passwd') {
      this.setData({
        'passwd_focus': true
      });
    } else if (e.target.id == 'shopid') {
      this.setData({
        'shopid_focus': true
      });
    } else if (e.target.id == 'phone') {
      this.setData({
        'phone_focus': true
      });
    }

  },
  inputBlur: function (e) {
    if (e.target.id == 'userid') {
      this.setData({
        'userid_focus': false
      });
    } else if (e.target.id == 'passwd') {
      this.setData({
        'passwd_focus': false
      });
    }
    else if (e.target.id == 'shopid') {
      this.setData({
        'shopid_focus': false
      });
    }
    else if (e.target.id == 'phone') {
      this.setData({
        'phone_focus': false
      });
    }
  },
  tapHelp: function (e) {
    if (e.target.id == 'help') {
      this.hideHelp();
    }
  },
  showHelp: function (e) {
    this.setData({
      'help_status': true
    });
  },
  hideHelp: function (e) {
    this.setData({
      'help_status': false
    });
  },
  Change_shopid: function (e) {
    this.setData({
      shopidValue: e.detail.value
    });
  },
  Change_shopname: function (e) {
    this.setData({
      shopnameValue: e.detail.value
    })
    var _this = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetShopName',
        ShopName: _this.data.shopname[e.detail.value]
      },
      success: (res) => {
        wx.hideToast();
        _this.setData({
          shopid: res.data.ShopID
        })
      },
      fail: function (res) {
        wx.hideToast();
        app.showErrorModal(res.errMsg);
      }
    })
  },
  Change_pian: function (e) {
    this.setData({
      pianValue: e.detail.value
    })
    var _this = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetPian',
        Pian: _this.data.pian[e.detail.value]
      },
      success: (res) => {
        wx.hideToast();
        _this.setData({
          shopid: res.data.ShopID,
          shopname: res.data.ShopName,
          sheng: res.data.Sheng,
          shi: res.data.Shi
        })
      },
      fail: function (res) {
        wx.hideToast();
        app.showErrorModal(res.errMsg);
      }
    })
  },
  Change_sheng: function (e) {
    this.setData({
      shengValue: e.detail.value
    })
    var _this = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetSheng',
        Sheng: _this.data.sheng[e.detail.value]
      },
      success: (res) => {
        wx.hideToast();
        _this.setData({
          shopid: res.data.ShopID,
          shopname: res.data.ShopName,
          shi: res.data.Shi
        })
      },
      fail: function (res) {
        wx.hideToast();
        app.showErrorModal(res.errMsg);
      }
    })
  },
  Change_shi: function (e) {
    this.setData({
      shiValue: e.detail.value
    })
    var _this = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetShi',
        Shi: _this.data.shi[e.detail.value]
      },
      success: (res) => {
        wx.hideToast();
        _this.setData({
          shopid: res.data.ShopID,
          shopname: res.data.ShopName
        })
      },
      fail: function (res) {
        wx.hideToast();
        app.showErrorModal(res.errMsg);
      }
    })
  },
  Change_shopsex: function(e){
    this.setData({
      shopsexValue:e.detail.value
    })
  },
});